using Newtonsoft.Json;
using Quartz;
using Quartz.Util;
using System;
using System.Collections.Specialized;
using System.IO;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.ScheduleJob.Engine;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Service
{
    public class RepositoryConfigService
    {
        private const string PropertyFileName = "ScheduleJob.Properties";
        private string _defaultPropertyFile = "";
        public string DefaultPropertyFile
        {
            get
            {
                if (string.IsNullOrEmpty(_defaultPropertyFile))
                {
                    _defaultPropertyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PropertyFileName);
                }
                return _defaultPropertyFile;
            }
        }
        public NameValueCollection GetConfig()
        {
            return ConfigHelper.GetQuartzConfig();
        }

        private NameValueCollection GetConfigFromLocalFile()
        {
            if (!File.Exists(DefaultPropertyFile))
            {
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobRTPEngineInitError, string.Format(CommonStringClassService.RepositoryConfigService_GetConfigFromLocalFile));
            }
            return GetProperties();

        }
        private NameValueCollection GetProperties()
        {
            var props = new NameValueCollection();
            try
            {
                PropertiesParser pp = PropertiesParser.ReadFromFileResource(DefaultPropertyFile);
                props = pp.UnderlyingProperties;
            }
            catch (Exception ex)
            {
                props = GetMySQLConfig();
            }
            return props;
        }


        private NameValueCollection GetSQLConfig()
        {
            NameValueCollection props = new NameValueCollection
            {
                //scheduler名字
                ["quartz.scheduler.instanceName"] = "MyScheduler",
                //线程池个数
                ["quartz.threadPool.threadCount"] = "20",
                //类型为JobStoreXT,事务
                //["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
                ["quartz.jobStore.type"] = "Quartz.JobStoreTX, Quartz",
                //JobDataMap中的数据都是字符串
                //["quartz.jobStore.useProperties"] = "true",
                ["quartz.jobStore.misfireThreshold"] = "5000",
                //数据源名称
                ["quartz.jobStore.dataSource"] = "myDS",
                //数据表名前缀
                ["quartz.jobStore.tablePrefix"] = "QRTZ_",
                //使用Sqlserver的Ado操作代理类
                //["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
                ["quartz.jobStore.driverDelegateType"] = "Quartz.SqlServerDelegate, Quartz",
                //数据源连接字符串
                ["quartz.dataSource.myDS.connectionString"] = "Server=127.0.0.1;Database=quartzDb;Uid=sa;Pwd=T@t.cn",
                //数据源的数据库
                ["quartz.dataSource.myDS.provider"] = "SqlServer",
                //序列化类型
                ["quartz.serializer.type"] = "json",//binary
                //自动生成scheduler实例ID，主要为了保证集群中的实例具有唯一标识
                ["quartz.scheduler.instanceId"] = "AUTO",
                //是否配置集群
                ["quartz.jobStore.clustered"] = "true",
            };
            return props;
        }
        private NameValueCollection GetMySQLConfig()
        {
            NameValueCollection props = new NameValueCollection
            {
                //scheduler名字
                ["quartz.scheduler.instanceName"] = "MyScheduler",
                //线程池个数
                ["quartz.threadPool.threadCount"] = "20",
                //类型为JobStoreXT,事务
                //["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
                ["quartz.jobStore.type"] = "Quartz.JobStoreTX, Quartz",
                //JobDataMap中的数据都是字符串
                //["quartz.jobStore.useProperties"] = "true",
                ["quartz.jobStore.misfireThreshold"] = "5000",
                //数据源名称
                ["quartz.jobStore.dataSource"] = "myDS",
                //数据表名前缀
                ["quartz.jobStore.tablePrefix"] = "QRTZ_",
                //使用Sqlserver的Ado操作代理类
                //["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
                ["quartz.jobStore.driverDelegateType"] = "Quartz.MySQLDelegate, Quartz",
                //数据源连接字符串
                ["quartz.dataSource.myDS.connectionString"] = "Data Source=139.219.7.200;Database=TQuartz;user ID=sa;password=T@t.cn;Port=3306",
                //数据源的数据库
                ["quartz.dataSource.myDS.provider"] = "MySql",
                //序列化类型
                ["quartz.serializer.type"] = "json",//binary
                //自动生成scheduler实例ID，主要为了保证集群中的实例具有唯一标识
                ["quartz.scheduler.instanceId"] = "AUTO",
                //是否配置集群
                ["quartz.jobStore.clustered"] = "true",
            };
            return props;
        }
    }
}
